package com.tanhua.dubbo.api.Impl;


import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.RecommendUserApi;
import com.tanhua.model.domain.UserLike;
import com.tanhua.model.mongoDb.RecommendUser;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-27 20:04
 * @Desc 推荐api实现类
 */
@DubboService(version = "1.0.0")
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public RecommendUser todayBest(Long userId) {
        //1.创建对象
        Criteria criteria = Criteria.where("toUserId").is(userId);
        //2.构建条件查询对象
        Query query = Query.query(criteria).
                with(Sort.by(Sort.Order.desc("score"))).limit(1);
        //3.调用Template返回值
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 首页推荐
     *
     * @param userId
     * @return java.util.List<com.tanhua.model.mongoDb.RecommendUser>
     * @Author Pan Rundong
     */
    @Override
    public List<RecommendUser> getPage(Long userId) {
        //1.创建对象
        Criteria criteria = Criteria.where("toUserId").is(userId);
        Query query = Query.query(criteria);
        //2.查询全部推荐用户
        query.with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> userList = mongoTemplate.find(query, RecommendUser.class);
        //3.返还map对象
        return userList;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return com.tanhua.model.vo.PageResult
     * @Author Pan Rundong
     */
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建Query对象
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        //总记录数
        Long count = mongoTemplate.count(query, RecommendUser.class);
        //配置分页参数
        query.skip((page-1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        //查询数据列表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page, pagesize, Integer.parseInt(count+""), recommendUsers);
    }

    /**
     * 根据佳人Id查询信息
     *
     * @param toUserId
     * @param userId
     * @return com.tanhua.model.mongoDb.RecommendUser
     * @Author Pan Rundong
     */
    @Override
    public RecommendUser findById(Long toUserId, Long userId) {
        //1.创建对象并设置条件
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));
        //2.mongodb查询并返回
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //3.判断
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(99d);
        }
        return recommendUser;
    }

    /**
     * 探花推荐
     *
     * @param userId
     * @param i
     * @return java.util.List<com.tanhua.model.mongoDb.RecommendUser>
     * @Author Pan Rundong
     */
    @Override
    public List<RecommendUser> cards(Long userId, int i) {
        //1.查询已喜欢的推荐人
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);

        //2.获取以喜欢推荐人Ids
        List<Long> ids = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);

        //3.设置条件，获得推荐人
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(ids);
            //指定查询条件
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(i)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();
    }
}
